package setting

import "time"

var sections = make(map[string]interface{})

type ServerSettingS struct {
	RunMode      string
	HttpPort     string
	Version      string
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
}

// 数据库配置

type DatabaseSettingS struct {
	DBType      string
	Username    string
	Password    string
	Host        string
	DBName      string
	TablePrefix string
	Charset     string
	ParseTime   bool
	MaxIdleTime int
	MaxOpenConn int
}

type LoggerSettingS struct {
	LogSavePath string
	LogFileName string
	LogFileExt  string
	LogLevel    string
}

// ReadSection 解析配置文件
func (s *Setting) ReadSection(k string, v interface{}) error {
	err := s.vp.UnmarshalKey(k, v)
	if err != nil {
		return err
	}

	if _, ok := sections[k]; !ok {
		sections[k] = v
	}
	return nil
}

// ReloadAllSections 读取所有配置
func (s *Setting) ReloadAllSections() error {
	for k, v := range sections {
		err := s.ReadSection(k, v)
		if err != nil {
			return err
		}
	}
	return nil
}
